package com.cyys.modules.system.model.user;

import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.SqlCondition;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.cyys.common.utils.StringUtil;
import com.cyys.common.validate.Edit;
import com.cyys.common.validate.Insert;
import com.cyys.modules.core.model.CoreUserBean;
import com.cyys.modules.core.sysParam.*;
import com.cyys.modules.system.model.org.Org;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.Data;

import javax.validation.constraints.NotBlank;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * User generated by hbm2java
 */
@Data
@TableName("sys_users")
public class User extends CoreUserBean {

	@TableField(condition = SqlCondition.LIKE)
	@NotBlank(message = "用户名不能为空",groups = {Edit.class, Insert.class})
	private String username;   //用户名

	@TableField(whereStrategy=FieldStrategy.NOT_EMPTY,updateStrategy=FieldStrategy.NOT_EMPTY)
	@JsonIgnore
	private String password;  //密码

	private String type;  //数据权限范围1、所有机构；2、本机构及下级机构；3、本机构；4、自定义机构；5、本人

	private String orgIds; //自定义范围权限
	
	private String disable;  //是否禁用
	
	private String phone;  //电话
	
	private String openId;  //对应微信编号
	
	private String idCard;  //身份证号
	
	private String isAuthentication;  //是否认证

	@TableField(condition = SqlCondition.LIKE)
	private String realName;  //真实姓名
	
	private Date dateLastLogin;

	private Integer pwdCount;

	private Date lockTime;

	private Integer lockStatus;

	private String orgId;

	private Date datePwdUpdate; //密码修改时间

	@TableField(exist = false)
	private String loginType;  //登录方式,1：账号密码登录，2：短信验证码登录

	@TableField(exist = false)
	@JsonIgnore
	private List<Role> roleList;  //用户角色

	@TableField(exist = false)
	@JsonIgnore
	private List<Menu> menuList;

	@TableField(exist = false)
	@JsonIgnore
	private List<Menu> menuListTree;

	@TableField(exist = false)
	@JsonIgnore
	private Set<String> permissions;

	public String getOrgName(){
		Boolean mutiOrg = Boolean.valueOf(SystemConfigureUtil.get(SystemConstants.MUTI_ORG));
		if(mutiOrg) {
			if (StringUtil.isBlank(orgId)) {
				return "";
			} else if (!orgId.equals("0") && OrgUtil.get(orgId)!=null) {
				return OrgUtil.get(orgId).getName();
			} else if (orgId.equals("0")) {
				return "顶级节点";
			}
		}
		return "";
	}

	@JsonIgnore
	public Org getOrg(){
		Boolean mutiOrg = Boolean.valueOf(SystemConfigureUtil.get(SystemConstants.MUTI_ORG));
		if(mutiOrg) {
			if (StringUtil.isNotBlank(orgId)) {
				return OrgUtil.get(orgId);
			}
		}
		return null;
	}

	@JsonIgnore
	public String getTypeName(){
		if(StringUtil.isNotBlank(type)){
			return ParamBasicUtil.getParamBasicTypeMap().get(ParamConstants.USER_TYPE).get(type);
		}
		return "";
	}

}
